var Toast = Swal.mixin({
    toast: true,
    position: 'center',
    showConfirmButton: false,
    timer: 3000
});
getCompany = function(pageNum){
    $.get({
        url:"/ERP/bss/company/getAll",
        async:false,
        data:{"pageNum":pageNum},
        dataType:"json",
        success:function (data) {
            var companyList=data.list;
            var str="";
            var  page="<li class='page-item'><a class='page-link disabled' href='javascript:void(0);'>&laquo;</a></li>";
            for (var i = 0; i <companyList.length; i++) {
                str+="<tr>"+
                    "<td>"+companyList[i].companyId+"</td>"+
                    "<td>"+companyList[i].companyName+"</td>"+
                    "<td>"+companyList[i].companyPhone+"</td>"+
                    "<td>"+companyList[i].companyAddr+"</td>"+
                    "<td>"+companyList[i].createTime+"</td>"+
                    "<td>"+companyList[i].updateTime+"</td>"+
                    "<td>"+"<button type='button' class='btn btn-block bg-gradient-primary ' data-toggle='modal' data-target='#modal-default'>" +
                    "<a href='javascript:void(0);' style='color:#fff;' class='editCompany' Id="+companyList[i].companyId+" >Edit</a>" +
                    "</button>"+"</td>"+
                    "<td>"+"<button type='button' class='btn btn-block bg-gradient-danger' >" +
                    "<a href='javascript:void(0);' style='color:#fff;'  class='delCompany' Id="+companyList[i].companyId+" onclick='delCompany("+companyList[i].companyId+")' >Delete</a>" +
                    "</button>"+"</td>"+
                    "</tr>";
            }
            $("#mytable tbody").html(str);
            for (let i = 0; i <data.pages; i++) {
                page+="<li class='page-item'><a class='page-link currentPages' href='javascript:void(0);'>"+(i+1)+"</a></li>"
            }
            page+="<li class='page-item disabled'><a class='page-link' href='javascript:void(0);'>&raquo;</a></li>";
            $("#paging").html(page);
        }
    })
    // delCompanys();
    editCompanys();
}
/*delCompanys = function(){
    $(".delCompany").each(function () {
        var Id =$(this).attr("Id");
        $(this).on('click',function () {
            if (confirm("Are you deleting?")) {
                $.get({
                    url:'/ERP/bss/company/del?id='+Id,
                    success:function () {
                        Toast.fire({
                            type: 'success',
                            title: 'congratulation you success!'
                        })
                        setTimeout(getCompany(),3000);
                    }
                })
            }else {
                return
            }
        })
    })
}*/
delCompany = function(id){
    if(confirm("你确定要删除吗？")){
        $.get({
            url:'/ERP/bss/company/del?id='+Id,
            success:function () {
                Toast.fire({
                    type: 'success',
                    title: 'congratulation you success!'
                })
                setTimeout(getCompany(),3000);
            }
        })
    }
}
editCompanys=function(){
    $(".editCompany").each(function (index,item) {
        var Id=$(this).attr("Id");
        $(this).click(function () {
            $.get({
                url:'/ERP/bss/company/find?id='+Id,
                async:false,
                success:function (data) {
                    $("#companyId").val(data.companyId);
                    $("#companyName").val(data.companyName);
                    $("#companyPhone").val(data.companyPhone);
                    $("#companyAddr").val(data.companyAddr);
                }
            })
        })
    })
}
$(function () {
    getCompany();
    // delCompanys();
    editCompanys();
    $(document).on('click','#editCompanySuccess',function() {
        $.post({
            url:'/ERP/bss/company/addOrUpd',
            data:$("#myForm").serialize(),
            success:function () {
                Toast.fire({
                    type: 'success',
                    title: 'congratulation you success!'
                })
                setTimeout(getCompany(),6000);
            }
        })
    });
    $(document).on('click','#addCompanySuccess',function() {
        $.post({
            url:'/ERP/bss/company/addOrUpd',
            data:$("#myForm1").serialize(),
            success:function () {
                Toast.fire({
                    type: 'success',
                    title: 'congratulation you success!'
                })
                setTimeout(getCompany(),6000);
            }
        })
    });
    $(".currentPages").each(function () {
        $(document).on('click','.currentPages',function () {
            getCompany($(this).text());
        })
    })
    $("#query").click(function () {
        $.get({
            url:'/ERP/bss/company/query',
            data:$("#myForm2").serialize(),
            success:function (data) {
                var companyList=data.list;
                var str="";
                var  page="<li class='page-item'><a class='page-link disabled' href='javascript:void(0);'>&laquo;</a></li>";
                for (var i = 0; i <companyList.length; i++) {
                    str+="<tr>"+
                        "<td>"+companyList[i].companyId+"</td>"+
                        "<td>"+companyList[i].companyName+"</td>"+
                        "<td>"+companyList[i].companyPhone+"</td>"+
                        "<td>"+companyList[i].companyAddr+"</td>"+
                        "<td>"+companyList[i].createTime+"</td>"+
                        "<td>"+companyList[i].updateTime+"</td>"+
                        "<td>"+"<button type='button' class='btn btn-block bg-gradient-primary ' data-toggle='modal' data-target='#modal-default'>" +
                        "<a href='javascript:void(0);' style='color:#fff;' class='editCompany' Id="+companyList[i].companyId+" >Edit</a>" +
                        "</button>"+"</td>"+
                        "<td>"+"<button type='button' class='btn btn-block bg-gradient-danger' >" +
                        "<a href='javascript:void(0);' style='color:#fff;'  class='delCompany' Id="+companyList[i].companyId+" onclick='delCompany("+companyList[i].companyId+")' >Delete</a>" +
                        "</button>"+"</td>"+
                        "</tr>";
                }
                $("#mytable tbody").html(str);
                for (let i = 0; i <data.pages; i++) {
                    page+="<li class='page-item'><a class='page-link currentPages' href='javascript:void(0);'>"+(i+1)+"</a></li>"
                }
                page+="<li class='page-item disabled'><a class='page-link' href='javascript:void(0);'>&raquo;</a></li>";
                $("#paging").html(page);
            }
        })
    })
})